package cn.itcast.wushangxiantu.BaseAdapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.gson.Gson;



import java.io.IOException;

import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.GuoJiaShenQing;
import cn.itcast.wushangxiantu.UserData.User;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class GuoJiaShenQingUserAdapter extends BaseAdapter {
    private Context context;
    private GuoJiaShenQing guoJiaShenQing;
    private ViewHolder viewHolder;
    private int gj_id;

    public GuoJiaShenQingUserAdapter(Context context) {
        this.context = context;
    }

    public GuoJiaShenQing getGuoJiaShenQing() {
        return guoJiaShenQing;
    }

    public GuoJiaShenQingUserAdapter(Context context, int gj_id) {
        this.context = context;
        this.gj_id = gj_id;
    }

    public void setGuoJiaShenQing(GuoJiaShenQing guoJiaShenQing) {
        this.guoJiaShenQing = guoJiaShenQing;
        Log.v("12312312","123123");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == guoJiaShenQing) {
            return 0;
        }
        return guoJiaShenQing.getGuoJiaShenQing().size();
    }

    @Override
    public Object getItem(int i) {
        return guoJiaShenQing.getGuoJiaShenQing().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.adapter_guojiachengyuan, null);
        viewHolder=new ViewHolder(view);
        viewHolder.tvZhiWei.setVisibility(View.INVISIBLE);
        viewHolder.tvName.setText(guoJiaShenQing.getGuoJiaShenQing().get(i).getName());
        viewHolder.tvDJ.setText("LV:"+guoJiaShenQing.getGuoJiaShenQing().get(i).getDj());
        viewHolder.tvGongXian.setText("申请时间"+guoJiaShenQing.getGuoJiaShenQing().get(i).getTime());
        viewHolder.btnSet.setText("同意");
        viewHolder.btnDelete.setText("拒绝");
        return view;
    }

    class ViewHolder implements View.OnClickListener {
        public View rootView;
        public TextView tvName;
        public TextView tvDJ;
        public TextView tvGongXian;
        public TextView tvZhiWei;
        public Button btnDelete;
        public Button btnSet;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvDJ = (TextView) rootView.findViewById(R.id.tvDJ);
            this.tvGongXian = (TextView) rootView.findViewById(R.id.tvGongXian);
            this.tvZhiWei = (TextView) rootView.findViewById(R.id.tvZhiWei);
            this.btnDelete = (Button) rootView.findViewById(R.id.btnDelete);
            this.btnSet = (Button) rootView.findViewById(R.id.btnSet);
            btnDelete.setOnClickListener(this);
            btnSet.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btnDelete:
                    GeneralDialog("仙盟申请","确定拒绝该玩家的仙盟申请嘛",1);
                    break;
                case R.id.btnSet:
                    GeneralDialog("仙盟申请","确定同意该玩家的仙盟申请嘛",0);
                    break;
            }
        }

        public void GeneralDialog(String head, String content, final int num) {
            /**
             * 普通对话框
             * 1标题
             * 2内容
             */
            AlertDialog dialog;
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                    .setTitle(head)

                    .setMessage(content)
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (num<=1){
                                setShengQing(Login.getUser().getUsernumber(), Login.getUser().getPassword(),num,tvName.getText().toString().trim());
                            }
                            dialogInterface.dismiss();
                        }
                    });
            dialog = builder.create();
            dialog.show();
        }

        private void setShengQing(String usernumber, String password, int num, String name) {
            OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "&need=setShengQing" + "&userNumber=" + usernumber + "&passWord=" + password + "&num=" + num + "&name=" + name, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    try {
                        Log.v("setShengQing",e.getMessage());
                    }catch (Exception e1){

                    }

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res=response.body().string();
                    Log.v("setShengQing",res);
                    Message message=new Message();
                    User user=new Gson().fromJson(res,User.class);
                    message.what=1;
                    message.obj=user;
                    handler.sendMessage(message);
                }
            });
        }
        Handler handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                switch (message.what){
                    case 1:
                        User user= (User) message.obj;
                        if (null!=user){
                            ToastUtil.toast(context,user.getStw());
                        }else {
                            ToastUtil.toast(context,"服务器炸了");
                        }
                        break;
                }
                return false;
            }
        });


    }
}
